package spring.test.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 时间工具类
 * @author likunran
 *
 */
public  class MarkDateUtil {

	
	/**
	 * 获取当前时间
	 * @return
	 */
	public static String GetNowDate(){   
	    String temp_str="";   
	    Date dt = new Date();   
	    //最后的aa表示“上午”或“下午”    HH表示24小时制    如果换成hh表示12小时制   
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss aa");   
	    temp_str=sdf.format(dt);   
	    return temp_str;   
	} 
	
	 public static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	    public static String DATE_PATTERN = "yyyy-MM-dd";
	    public static String TIME_PATTERN = "HH:mm:ss";
	    public static String COOKIE_TIME_PATTERN = "yyyyMMddHHmmss";

	    /**
	     * 将String类型转换成Date类型
	     *
	     * @param dateString
	     * @return
	     */

	    public static Date stringToDate(String dateString) {

	        Date date = new Date();
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        try {
	            date = sdf.parse(dateString);
	        } catch (ParseException e) {
	            e.printStackTrace();
	        }
	        return date;

	    }

	    /**
	     * 将Date类型转换成String类型
	     *
	     * @param date
	     * @return
	     */
	    public static String dateToString(Date date) {
	        return dateToString(date, "yyyy-MM-dd HH:mm:ss.SSS");
	    }

	    /**
	     * 根据指定格式将date转换为string
	     *
	     * @param date
	     * @param format
	     * @return
	     */
	    public static String dateToString(Date date, String format) {
	        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
	        return simpleDateFormat.format(date);
	    }

	    /**
	     * 根据当前日期得到以前的日期
	     *
	     * @param curDate
	     * @return
	     */
	    public static Date getDate(Date curDate, int number) {
	        Calendar calendar = Calendar.getInstance();
	        calendar.setTime(curDate);
	        calendar.add(Calendar.DAY_OF_YEAR, -number);
	        Date date = calendar.getTime();
	        return date;
	    }

	    /**
	     * 根据当前日期得到以后的日期
	     *
	     * @param curDate
	     * @return
	     */
	    public static Date getDateAfter(String type, Date curDate, int number) {
	        Calendar calendar = Calendar.getInstance();
	        calendar.setTime(curDate);
	        if (type.equals("year")) {
	            calendar.add(Calendar.YEAR, +number);
	        } else if (type.equals("month")) {
	            calendar.add(Calendar.MONTH, +number);
	        } else if (type.equals("dayOfMonth")) {
	            calendar.add(Calendar.DAY_OF_MONTH, +number);
	        } else if (type.equals("h")) {
	            calendar.add(Calendar.HOUR, +number);
	        } else if (type.equals("m")) {
	            calendar.add(Calendar.MINUTE, +number);
	        } else if (type.equals("s")) {
	            calendar.add(Calendar.SECOND, +number);
	        }
	        Date dateAfter = calendar.getTime();
	        return dateAfter;
	    }

	    /**
	     * 时间格式化
	     *
	     * @param timeStr
	     * @return
	     */
	    public static String formatDate(String timeStr) {
	        int colonCount = 0;
	        for (int i = 0; i < timeStr.length(); i++) {
	            char str = timeStr.charAt(i);

	            if (str == ':') {
	                colonCount++;
	            }
	        }
	        System.out.println("colonCount=" + colonCount);
	        if (colonCount == 0) { // 时间格式为yyyy-MM-dd时
	            timeStr += " 00:00:00.0000000";
	        } else if (colonCount == 1) { // 时间格式为yyyy-MM-dd HH:mm时
	            timeStr += ":00.0000000";
	        } else if (colonCount == 2) { // 时间格式为yyyy-MM-dd HH:mm:ss时
	            timeStr += ".0000000";
	        }
	        return timeStr;
	    }

	    public static int getDatePattern(String dateStr, int pattern) {
	        Calendar calendar = new GregorianCalendar();
	        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	        try {
	            calendar.setTime(format.parse(dateStr));
	        } catch (ParseException e) {
	            e.printStackTrace();
	        }
	        return calendar.get(pattern);
	    }

	    public static String getDate(Date aDate, String pattern) {
	        SimpleDateFormat df = new SimpleDateFormat(pattern);
	        return df.format(aDate);
	    }

	    public static String getDate(long aDate, String pattern) {
	        return getDate(new Date(aDate), pattern);
	    }

	    public static Date convertStringToDate(String pattern, String strDate) throws ParseException {
	        SimpleDateFormat df;
	        Date date;
	        df = new SimpleDateFormat(pattern);
	        try {
	            date = df.parse(strDate);
	        } catch (ParseException pe) {
	            throw new ParseException(pe.getMessage(), pe.getErrorOffset());
	        }
	        return (date);
	    }

	    public static boolean isStrDate(String strDate, String pattern) {
	        Date date = null;
	        try {
	            SimpleDateFormat df = new SimpleDateFormat(pattern);
	            date = df.parse(strDate);
	        } catch (Exception ex) {
	            return false;
	        }
	        if (date == null)
	            return false;
	        return true;
	    }

	    /**
	     * 获取当前系统时间
	     *
	     * @return
	     */
	    public static Date getNowEasyDate() {

	        return getNowEasyDate(0);
	    }

	    /**
	     * 获取时间格式为YYY-MM-DD 00：00：00
	     *
	     * @param reset
	     * @return
	     */
	    public static Date getNowEasyDate(int reset) {

	        Calendar curr = Calendar.getInstance();

	        if (reset > 0) {

	            curr.add(Calendar.DATE, -reset);
	        }
	        curr.set(curr.get(Calendar.YEAR), curr.get(Calendar.MONTH), curr.get(Calendar.DAY_OF_MONTH), 00, 00, 00);

	        return curr.getTime();
	    }

	    /**
	     * 计算两个时期之间相差天数
	     *
	     * @param startDate
	     * @param endDate
	     * @return
	     */
	    public static long displayDate(Date startDate, Date endDate) {
	        long leaveDay = 0L;
	        long templong = 0L;
	        final long tempday = 1000 * 60 * 60 * 24;
	        templong = endDate.getTime() - startDate.getTime();
	        leaveDay = templong / tempday;
	        if (templong - leaveDay * tempday > 1000) {
	            leaveDay = leaveDay + 1;
	        }

	        leaveDay = leaveDay < 0 ? 0 : leaveDay;
	        return leaveDay;
	    }

	    /*
	        根据周获取周一和周日的日期
	     */
	    public static String[] getFirstLastDayByWeekDay(int week) {
	        String[] day = new String[2];
	        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);

	        Calendar c = Calendar.getInstance();

	        c.set(Calendar.WEEK_OF_YEAR, week);
	        c.setFirstDayOfWeek(Calendar.MONDAY);
	        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
	        day[0] = sdf.format(c.getTime());
	        c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
	        day[1] = sdf.format(c.getTime());
	        return day;
	    }

	    /*
	       根据月获取本月第一和最后一天
	     */
	    public static String[] getFirstLastDayByMonth(int month) {
	        String[] day = new String[2];
	        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);

	        Calendar c = Calendar.getInstance();

	        c.set(Calendar.DAY_OF_MONTH, 1);
	        c.set(Calendar.MONTH, month);   //月份
	        day[0] = sdf.format(c.getTime());


	        c.add(Calendar.MONTH, 1);// 加一个月，变为下月的1号
	        c.add(Calendar.DATE, -1);// 减去一天，变为当月最后一天

	        day[1] = sdf.format(c.getTime());
	        return day;
	    }
}
